package com.cvte.boe.media.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cvte.boe.media.R;
import com.cvte.boe.media.controller.LRUThumbManager;
import com.cvte.boe.media.model.LocalMediaInfo;
import com.cvte.boe.media.utils.constant.Constant;
import com.cvte.boe.media.views.widget.MarqueeTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuguitong on 2018/4/11.
 */
public class FileBrowserAdapter extends BaseAdapter{
    private Context mContext;

    private List<LocalMediaInfo> mMediaInfoList;
    private int mCurrentPosition;

    public FileBrowserAdapter(Context mContext, List<LocalMediaInfo> mediaInfoList) {
        this.mContext = mContext;
        this.mMediaInfoList = new ArrayList<>();
        this.mMediaInfoList.clear();
        this.mMediaInfoList.addAll(mediaInfoList);
        mCurrentPosition = 0;
    }

    @Override
    public int getCount() {
        if(mMediaInfoList != null){
            return mMediaInfoList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mMediaInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MediaViewHolder viewHolder = null;
        LocalMediaInfo localMediaInfo = mMediaInfoList.get(position);
        if(convertView == null){
            viewHolder = new MediaViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_media_file_browser,null);
            viewHolder.ivMediaIcon = (ImageView)convertView.findViewById(R.id.iv_media_file_icon);
            viewHolder.tvNmae = (MarqueeTextView)convertView.findViewById(R.id.tv_media_file_name);
            viewHolder.tvInfo = (MarqueeTextView)convertView.findViewById(R.id.tv_media_file_info);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (MediaViewHolder) convertView.getTag();
        }
        int type = localMediaInfo.getFileType();
        int resId;
        String info;
        Bitmap bitmap;
        switch (type){
            case Constant.MediaType.VIDEO:
                bitmap = LRUThumbManager.getInstance().getImageThumb(localMediaInfo.getFileAllName());
                if(bitmap != null){
                    viewHolder.ivMediaIcon.setImageBitmap(bitmap);
                }else {
                    resId = R.drawable.file_type_video;
                    viewHolder.ivMediaIcon.setImageResource(resId);
                }
                break;
            case Constant.MediaType.AUDIO:
                resId = R.drawable.file_type_audio;
                viewHolder.ivMediaIcon.setImageResource(resId);
                break;
            case Constant.MediaType.IMAGE:
                bitmap = LRUThumbManager.getInstance().getImageThumb(localMediaInfo.getFileAllName());
                if(bitmap != null){
                    viewHolder.ivMediaIcon.setImageBitmap(bitmap);
                }else {
                    resId = R.drawable.file_type_picture;
                    viewHolder.ivMediaIcon.setImageResource(resId);
                }
                break;
            case Constant.MediaType.FOLDER:
                resId = R.drawable.file_type_folder;
                viewHolder.ivMediaIcon.setImageResource(resId);
                break;
            default:
                resId = R.drawable.file_type_file_unknow;
                viewHolder.ivMediaIcon.setImageResource(resId);
                break;
        }
        if(type == Constant.MediaType.FOLDER){
            info = Integer.toString(localMediaInfo.getFiles()) + mContext.getString(R.string.dir_file_info);
        }else{
            info = localMediaInfo.getFileSizeStr();
        }
        viewHolder.tvNmae.setText(localMediaInfo.getFileName());
        viewHolder.tvInfo.setText(info);
        return convertView;
    }
    public  void setSelectPosition(int position){
        mCurrentPosition = position;
    }
    public void updateDataAndView(List<LocalMediaInfo> mediaInfoList){
        mMediaInfoList.clear();
        mMediaInfoList.addAll(mediaInfoList);
        this.notifyDataSetChanged();
    }
    class MediaViewHolder {
        ImageView ivMediaIcon;
        MarqueeTextView tvNmae;
        MarqueeTextView tvInfo;
    }
}
